package com.tgit.sso.paltform.config;

import java.util.Properties;

import com.tgit.sso.core.conf.SsoConf;
import com.tgit.sso.core.conf.SsoException;
import com.github.pagehelper.PageHelper;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

/**
 * mybatis配置
 * @author 范再军
 * 2018年9月14日
 */
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.tgit.sso.paltform.**.mapper")
public class MyBatisConfig  implements TransactionManagementConfigurer{
    private final static Logger LOGGER= LoggerFactory.getLogger(MyBatisConfig.class);
    
	@Autowired
	private DruidConfig druidConfig;
	
	@Bean
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
         try {
			return new DataSourceTransactionManager(druidConfig.mysqlDataSource());
		} catch (Exception e) {
		    LOGGER.error(SsoConf.ERROR_SSO.DB_CONNECT_MSG,e);
			throw new SsoException(SsoConf.ERROR_SSO.DB_CONNECT_MSG);
		}
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() {
    	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        try {
			sqlSessionFactoryBean.setDataSource(druidConfig.mysqlDataSource());
		} catch (Exception e1) {
		    LOGGER.error(SsoConf.ERROR_SSO.DB_CONNECT_MSG,e1);
		    throw new SsoException(SsoConf.ERROR_SSO.DB_CONNECT_MSG);
		}
        //没用到
        sqlSessionFactoryBean.setTypeAliasesPackage("com.tgit.sso.paltform.**.mapper");

        //分页插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageHelper.setProperties(properties);

        //添加插件
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});

        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/com/tgit/sso/**/*Mapper.xml"));
            return sqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            LOGGER.error(SsoConf.ERROR_SSO.DB_CONNECT_MSG,e);
            throw new SsoException(SsoConf.ERROR_SSO.DB_CONNECT_MSG);
        }
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
